package com.springcloud.controller;

import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.springcloud.IDubboService;
import com.springcloud.Product;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description
 * @Author 顾志杰
 * @Date 2021/10/19 09:32
 */
@RestController
public class Controller {
    @Reference(
            loadbalance = "roundrobin"
    )
    private IDubboService iDubboService;

    @PostMapping("/publish")
    @SentinelResource(value = "com.springcloud.controller:publish",
            entryType = EntryType.OUT,
            blockHandler = "helloBlockHandier",
            fallback = "fallback"
    )
    public Product publish(@RequestParam String name) {
        Product product = new Product();
        product.setName(name);
        if("111".equals(name)){
            throw new RuntimeException("111");
        }
        return iDubboService.publish(product);
    }

    public Product helloBlockHandier(String name,BlockException blockException) {
        System.out.println("降级");
        Product product = new Product();
        product.setName("降级");
        return product;
    }

    public Product fallback(String name,Throwable throwable) {
        System.out.println("异常");
        Product product = new Product();
        product.setName("异常");
        return product;
    }
}
